import { ref, computed, type Ref } from 'vue';

/**
 * 全局状态管理 - 分析数据状态
 */
class AnalysisStore {
  private static _instance: AnalysisStore | null = null;

  // 当前选中的分析文件路径
  private _currentFilePath: Ref<string> = ref('');

  // 数据版本号，用于强制刷新
  private _dataVersion: Ref<number> = ref(0);

  // 加载状态
  private _isLoading: Ref<boolean> = ref(false);

  private constructor() {}

  /**
   * 获取单例实例
   */
  static getInstance(): AnalysisStore {
    if (!this._instance) {
      this._instance = new AnalysisStore();
    }
    return this._instance;
  }

  /**
   * 获取当前分析文件路径
   */
  get currentFilePath(): string {
    return this._currentFilePath.value;
  }

  /**
   * 设置当前分析文件路径
   * @param filePath - 文件路径
   */
  setCurrentFilePath(filePath: string): void {
    if (this._currentFilePath.value !== filePath) {
      this._currentFilePath.value = filePath;
      this.incrementDataVersion();

      console.log(`🔄 全局状态更新 - 当前文件路径: ${filePath}, 版本: ${this.dataVersion}`);
    }
  }

  /**
   * 获取数据版本号
   */
  get dataVersion(): number {
    return this._dataVersion.value;
  }

  /**
   * 递增数据版本号（用于强制刷新组件）
   */
  incrementDataVersion(): void {
    this._dataVersion.value += 1;
  }

  /**
   * 获取组件Key（基于路径和版本）
   * @param componentName - 组件名称
   * @returns 组件key
   */
  getComponentKey(componentName: string): string {
    const pathKey = this._currentFilePath.value || 'default';
    return `${componentName}-${pathKey.replace(/[^a-zA-Z0-9]/g, '_')}-${this._dataVersion.value}`;
  }

  /**
   * 获取加载状态
   */
  get isLoading(): boolean {
    return this._isLoading.value;
  }

  /**
   * 设置加载状态
   * @param loading - 是否加载中
   */
  setLoading(loading: boolean): void {
    this._isLoading.value = loading;
  }

  /**
   * 获取响应式的当前文件路径
   */
  get reactiveFilePath(): Ref<string> {
    return this._currentFilePath;
  }

  /**
   * 获取响应式的数据版本
   */
  get reactiveDataVersion(): Ref<number> {
    return this._dataVersion;
  }

  /**
   * 获取响应式的加载状态
   */
  get reactiveIsLoading(): Ref<boolean> {
    return this._isLoading;
  }

  /**
   * 计算属性：获取简短的文件名
   */
  get shortFileName(): string {
    if (!this._currentFilePath.value) return '未选择';
    const parts = this._currentFilePath.value.split('/');
    return parts[parts.length - 1] || this._currentFilePath.value;
  }

  /**
   * 重置状态
   */
  reset(): void {
    this._currentFilePath.value = '';
    this._dataVersion.value = 0;
    this._isLoading.value = false;
    console.log('🔄 全局状态已重置');
  }

  /**
   * 获取状态信息（用于调试）
   */
  getStateInfo(): {
    currentFilePath: string;
    dataVersion: number;
    isLoading: boolean;
    shortFileName: string;
  } {
    return {
      currentFilePath: this.currentFilePath,
      dataVersion: this.dataVersion,
      isLoading: this.isLoading,
      shortFileName: this.shortFileName,
    };
  }
}

// 导出单例实例
export const analysisStore = AnalysisStore.getInstance();

/**
 * Composition API Hook - 使用分析状态
 */
export function useAnalysisStore() {
  return {
    // 状态
    currentFilePath: computed(() => analysisStore.currentFilePath),
    dataVersion: computed(() => analysisStore.dataVersion),
    isLoading: computed(() => analysisStore.isLoading),
    shortFileName: computed(() => analysisStore.shortFileName),

    // 方法
    setCurrentFilePath: (filePath: string) => analysisStore.setCurrentFilePath(filePath),
    incrementDataVersion: () => analysisStore.incrementDataVersion(),
    setLoading: (loading: boolean) => analysisStore.setLoading(loading),
    getComponentKey: (componentName: string) => analysisStore.getComponentKey(componentName),
    reset: () => analysisStore.reset(),
    getStateInfo: () => analysisStore.getStateInfo(),
  };
}
